library(foreign)
library(iotools)

#This script uses the output from eba_pth_B.py,


#Set working directory
#setwd()
dir.path <- '/eba.out.pthB/'

deci <- function(x, k, center = TRUE) {
	a <- format(round(x, k), nsmall=k)
	return(a)
}

#----------------------------------------------------
#Table B3
#----------------------------------------------------

ids.bsc_0 <- read.csv.raw(paste(dir.path, 'ID_Refs_0.csv', sep = ''))
ids.bsc_1 <- read.csv.raw(paste(dir.path, 'ID_Refs_1.csv', sep = ''))
ids.bsc_2 <- read.csv.raw(paste(dir.path, 'ID_Refs_2.csv', sep = ''))
ids.bsc_3 <- read.csv.raw(paste(dir.path, 'ID_Refs_3.csv', sep = ''))
ids.bsc_4 <- read.csv.raw(paste(dir.path, 'ID_Refs_4.csv', sep = ''))
ids.bsc_5 <- read.csv.raw(paste(dir.path, 'ID_Refs_5.csv', sep = ''))
ids <- rbind(ids.bsc_0, ids.bsc_1, ids.bsc_2, ids.bsc_3, ids.bsc_4, ids.bsc_5)

tableRows.fl <- NULL
tableRows.sc <- NULL
pt.rep <- c('-2.0', '-1.5', '-1.0', '-0.5', '0.0', '1.0', '1.5', '2.0', '2.5', '3.0')

for (.rep in pt.rep){
	print (paste(.rep, 'fl'))
	failed.bsc.pt_1 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_0.csv', sep = ''))
	failed.bsc.pt_1 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_1.csv', sep = ''))
	failed.bsc.pt_2 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_2.csv', sep = ''))
	failed.bsc.pt_3 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_3.csv', sep = ''))
	failed.bsc.pt_4 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_4.csv', sep = ''))
	failed.bsc.pt_5 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_pt_', .rep, '_5.csv', sep = ''))

	failed.bsc.pt <- rbind(
		failed.bsc.pt_0,
		failed.bsc.pt_1,
		failed.bsc.pt_2,
		failed.bsc.pt_3,
		failed.bsc.pt_4,
		failed.bsc.pt_5
		)
	nregressions.heter <- nrow(failed.bsc.pt)

	failed.bsc.pt <- failed.bsc.pt[!is.na(failed.bsc.pt$pVal),]


	cf.shnegtot.fl.pt.bsc <- sum(failed.bsc.pt$Coef < 0)/nrow(failed.bsc.pt)
	cf.shnegsig.fl.pt.bsc <- sum(failed.bsc.pt$Coef < 0 & failed.bsc.pt$pVal < 0.05)/nrow(failed.bsc.pt)
	cf.shpostot.fl.pt.bsc <- sum(failed.bsc.pt$Coef > 0)/nrow(failed.bsc.pt)
	cf.shpossig.fl.pt.bsc <- sum(failed.bsc.pt$Coef > 0 & failed.bsc.pt$pVal < 0.05)/nrow(failed.bsc.pt)
	cf.average.fl.pt.bsc <- mean(failed.bsc.pt$Coef)
	se.average.fl.pt.bsc <- mean(failed.bsc.pt$SE)
	tv.average.fl.pt.bsc <- mean(failed.bsc.pt$tVal)

	addRow.fl <- c(
				deci(cf.shnegtot.fl.pt.bsc, 3),
				deci(cf.shnegsig.fl.pt.bsc, 3),
				deci(cf.shpostot.fl.pt.bsc, 3),
				deci(cf.shpossig.fl.pt.bsc, 3),
				deci(cf.average.fl.pt.bsc, 2),
				deci(se.average.fl.pt.bsc, 2),
				deci(tv.average.fl.pt.bsc, 2)
				)
	tableRows.fl <- rbind(tableRows.fl, addRow.fl)

	success.bsc.pt_1 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_0.csv', sep = ''))
	success.bsc.pt_1 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_1.csv', sep = ''))
	success.bsc.pt_2 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_2.csv', sep = ''))
	success.bsc.pt_3 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_3.csv', sep = ''))
	success.bsc.pt_4 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_4.csv', sep = ''))
	success.bsc.pt_5 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_pt_', .rep, '_5.csv', sep = ''))
	nrow(success.bsc.pt_5)

	success.bsc.pt <- rbind(
		success.bsc.pt_0,
		success.bsc.pt_1,
		success.bsc.pt_2,
		success.bsc.pt_3,
		success.bsc.pt_4,
		success.bsc.pt_5
		)

	success.bsc.pt <- success.bsc.pt[!is.na(success.bsc.pt$pVal),]

	cf.shnegtot.sc.pt.bsc <- sum(success.bsc.pt$Coef < 0)/nrow(success.bsc.pt)
	cf.shnegsig.sc.pt.bsc <- sum(success.bsc.pt$Coef < 0 & success.bsc.pt$pVal < 0.05)/nrow(success.bsc.pt)
	cf.shpostot.sc.pt.bsc <- sum(success.bsc.pt$Coef > 0)/nrow(success.bsc.pt)
	cf.shpossig.sc.pt.bsc <- sum(success.bsc.pt$Coef > 0 & success.bsc.pt$pVal < 0.05)/nrow(success.bsc.pt)
	cf.average.sc.pt.bsc <- mean(success.bsc.pt$Coef)
	se.average.sc.pt.bsc <- mean(success.bsc.pt$SE)
	tv.average.sc.pt.bsc <- mean(success.bsc.pt$tVal)

	addRow.sc <- c(
				deci(cf.shnegtot.sc.pt.bsc, 3),
				deci(cf.shnegsig.sc.pt.bsc, 3),
				deci(cf.shpostot.sc.pt.bsc, 3),
				deci(cf.shpossig.sc.pt.bsc, 3),
				deci(cf.average.sc.pt.bsc, 2),
				deci(se.average.sc.pt.bsc, 2),
				deci(tv.average.sc.pt.bsc, 2)
				)
	tableRows.sc <- rbind(tableRows.sc, addRow.sc)
}

the_header <- cbind(
	c('Variable', '', ''),
	c('Pre-Treatment', 'Level of', 'Repression'),
	c('Prop. Effects', 'Negative', ''),
	c('Prop. Effects', 'Negative and', 'p<0.05'),
	c('Prop. Effects', 'Positive', ''),
	c('Prop. Effects', 'Positive and', 'p<0.05'),
	c('Mean', 'Coef.', ''),
	c('Mean', 'SE', ''),
	c('Mean', 't-Value', '')
	)

rownames.cus <- c('-2.0', '-1.5', '-1.0', '-0.5', '0.0', '1.0', '1.5', '2.0', '2.5', '3.0')

tableRows.fl.out <- cbind(rownames.cus,
	tableRows.fl)
tableRows.sc.out <- cbind(rownames.cus,
	tableRows.sc)

frst_column <- c(
		'Failed Coup',
			rep('', length(pt.rep)-1),
		'Successful Coup',
			rep('', length(pt.rep)-1))

eba.heter.table_t <- rbind(tableRows.fl.out, tableRows.sc.out)
eba.heter.table <- cbind(frst_column, eba.heter.table_t)
eba.heter.table.out <- rbind(the_header, eba.heter.table)
rownames(eba.heter.table.out) <- NULL
TableB3 <- print(
			 xtable(eba.heter.table.out,
			 caption = paste("Marginal Effect of Failed Coups and Successful Coups for Different Pre-Treatment Levels of Repression (EBA)", sep = ''),
			 align = 'l|l|c|cc|cc|ccc|',
			  label = 'ext:bnds:marg'),
		 include.rownames = FALSE,
		 include.colnames = FALSE,
		 caption.placement = 'top',
		 table.placement = "H",
		 size = "\\scriptsize",
		 hline.after = c(-1, 3, 13, nrow(eba.heter.table.out)),
		 floating = FALSE)

